<?php

/*
  VoIP Wholesale 1.0
  ------------------
  Document   : ajax
  Created on : Sep 25, 2011, 11:50:06 AM
  Author     : Milos Jovanovic <miloscode@gmail.com>
  Description:

 */

/*
 * *********************************************************
 * ## Module controllers must extend MD_Controller class ##
 * *********************************************************
 */
class Ajax extends MD_Controller {
    
public function __construct()
{
    parent::__construct();
    
	
	/*
	 * ******************************************************************
	 * CHECK IF MODULE IS INSTALLED PROPERLY
	 * ******************************************************************
	 */
	$this->MODULE->config->load('module_config');	
	$short_name = $this->MODULE->config->item('short_name');
	$settings_table = $this->MODULE->config->item('settings_table');	 
	if(!$this->PACKAGE->security->app_modules->module_exist($short_name) || empty($short_name) || empty($settings_table))
	{
		$this->PACKAGE->errors->generate->code("001")->stop();
	}
	
            /*
             * ********************************************
             * LOAD LANGUAGE FILES FOR THIS MODULE ONLY
             * notice that some language files have been
             * loaded within MY_Controller core class
             * and those language files are common for all
             * modules
             * ********************************************
             */    
    
            $this->MODULE->lang->load('module_texts',LANGUAGE);
            
            /*
             * ********************************************
             * LOAD PACKAGES
             * ********************************************
             */
            $this->packages->add('regional');   
      
            /*
             * ********************************************
             * LOAD MODELS
             * ********************************************
             */
            $this->PACKAGE->regional->load->model('countries');
            $this->PACKAGE->regional->load->model('timezone');
            
}

public function add_new_route($numbering_plan)
{
	if($numbering_plan == "external")
	{
		$this->MODULE->load->model('module_mod_external');
    
		$response = $this->MODULE->module_mod_external->add_new_route($this->input->post('data'));
	}
	elseif($numbering_plan == "internal")
	{
		$this->MODULE->load->model('module_mod_internal');
    
		$response = $this->MODULE->module_mod_internal->add_new_route($this->input->post('data'));	
	}
	elseif($numbering_plan == "special")
	{
		$this->MODULE->load->model('module_mod_special');
    
		$response = $this->MODULE->module_mod_special->add_new_route($this->input->post('data'));	
	}
    
    print $response;
    
}

public function save_settings()
{
    $this->MODULE->load->model('module_mod_settings');
    
    $response = $this->MODULE->module_mod_settings->save_settings($this->input->post('data'));
    
    print $response;
    
}

 
public function close_route($numbering_plan)
{
	if($numbering_plan == "external")
	{
		$this->MODULE->load->model('module_mod_external');
    
		$response = $this->MODULE->module_mod_external->close_route($this->input->post('data'));
	}
	elseif($numbering_plan == "internal")
	{
		$this->MODULE->load->model('module_mod_internal');
    
		$response = $this->MODULE->module_mod_internal->close_route($this->input->post('data'));	
	}
	elseif($numbering_plan == "special")
	{
		$this->MODULE->load->model('module_mod_special');
    
		$response = $this->MODULE->module_mod_special->close_route($this->input->post('data'));	
	}
    
    print $response;
    
} 

public function load_country_routes_external($country_id,$route_name = false)
{
	$sql = "SELECT * FROM `numbering_plan_external` WHERE `country_id` = '{$country_id}' ORDER BY `name` ASC";
	
	$query = $this->db->query($sql);

	$routes = array();
	foreach($query->result_array() as $entry)
	{	
		if(!in_array($entry['name'],@$routes))
		{
			$routes[] = $entry['name'];
		}
	}
	
	$response = array();
	if(count($routes) > 0)
	{
		foreach($routes as $route)
		{
			$text = "<option value='".$route."'";
			if($route_name!=false && $route_name==$route)
			{
			$text .= "selected = 'selected' ";
			}
			$text .= ">".$route."</option>";
			
			$response[] = $text;
		}
	}
	if(count($response) > 0)
	{
		$response = implode("\n\r",$response);
		$response = "<option value='none'>".lang('term_route_name')."</option>" . $response;
	}
	else
	{
		$response = "false";
	}
	
	print $response;
}

public function load_country_routes_internal($country_id,$route_name = false)
{
	$sql = "SELECT * FROM `numbering_plan_internal` WHERE `country_id` = '{$country_id}' ORDER BY `name` ASC";
	
	$query = $this->db->query($sql);

	$routes = array();
	foreach($query->result_array() as $entry)
	{	
		if(!in_array($entry['name'],@$routes))
		{
			$routes[] = $entry['name'];
		}
	}
	
	$response = array();
	if(count($routes) > 0)
	{
		foreach($routes as $route)
		{
			$text = "<option value='".$route."'";
			if($route_name!=false && $route_name==$route)
			{
			$text .= "selected = 'selected' ";
			}
			$text .= ">".$route."</option>";
			
			$response[] = $text;
		}
	}
	if(count($response) > 0)
	{
		$response = implode("\n\r",$response);
		$response = "<option value='none'>".lang('term_route_name')."</option>" . $response;
	}
	else
	{
		$response = "false";
	}
	
	print $response;
}

public function load_country_routes_special($country_id,$route_name = false)
{
	$sql = "SELECT * FROM `numbering_plan_special` WHERE `country_id` = '{$country_id}' ORDER BY `name` ASC";
	
	$query = $this->db->query($sql);

	$routes = array();
	foreach($query->result_array() as $entry)
	{	
		if(!in_array($entry['name'],@$routes))
		{
			$routes[] = $entry['name'];
		}
	}
	
	$response = array();
	if(count($routes) > 0)
	{
		foreach($routes as $route)
		{
			$text = "<option value='".$route."'";
			if($route_name!=false && $route_name==$route)
			{
			$text .= "selected = 'selected' ";
			}
			$text .= ">".$route."</option>";
			
			$response[] = $text;
		}
	}
	if(count($response) > 0)
	{
		$response = implode("\n\r",$response);
		$response = "<option value='none'>".lang('term_route_name')."</option>" . $response;
	}
	else
	{
		$response = "false";
	}
	
	print $response;
}

public function simulate()
{
	$post = $this->input->post('data');
	
	$destination_number = $post['destination_number'];
		
	$this->MODULE->load->model('module_mod_external');	
	$this->MODULE->load->model('module_mod_internal');	
	$this->MODULE->load->model('module_mod_special');	
	
	$this->PACKAGE->regional->load->model('countries');	
	$this->PACKAGE->core->load->library('calculations');	
	
	$this->packages->add('routing');
	$this->PACKAGE->routing->load->model('mod_routing');
	$this->PACKAGE->routing->load->library('numberingplan/numberingplan_routing');
	
		
	// get country
	$country = $this->PACKAGE->regional->countries->get_country_as_array_from_prefix($destination_number);
	
	/* 
	 * **********************************************
	 * VERIFY ENTERED DATA
	 * **********************************************
	 */
	// empty / wrong destination number
	if($destination_number == 0)
	{
		$response = "false|".lang('msg_wrong_destination_number_format');
	}
	else if(preg_match('/[a-zA-Z]/', $destination_number))
     {
	 	$response = "false|".lang('msg_letters_in_destination_number');                            
     }	
	// no country found
	elseif(empty($country['country_name']))
	{
		$response = "false|".lang('msg_destination_number_does_not_have_country');		
	}
	else
	{
	/* 
	 * **********************************************
	 * ENTERED DATA SEEM CORRECT - PERFORM SIMULATION
	 * **********************************************
	 */	

		/* ***********************************************************************
		 * METHOD I
		 * --------
		 * PARSING ARRAY OF ALL THE ROUTES THAT BELONG TO THE DETECTED
		 * COUNTRY BY USING METHOD get_longest_match() OF THE calculations LIBRARY
		 * FROM core PACKAGE
		 * ***********************************************************************/
		 
		// TIMING STARTS BEFORE FINDING THE ROUTES
		$start_time = microtime(TRUE);
			 
			 
		$routes_array = $this->PACKAGE->routing->numberingplan_routing->route($destination_number,'parsing_arrays',$country['id']);			 
		
		$ext_route = $routes_array['ext_route'];
		$int_route = $routes_array['int_route'];
		$spe_route = $routes_array['spe_route'];
		
					
		// TIMING ENDS AFTER ROUTES ARE FOUND (FURTHER RESPONSE PROCESSING IS NOT INCLUDED IN TIME DIFFERENCE!!!)
		$end_time = microtime(TRUE);
	
	
		// CALCULATE TIME DIFFERENCE
		$time_difference = $end_time - $start_time;		
		$time_difference = round($time_difference,6);
	
		// PREPARE RESPONSE FOR THE FIRST METHOD
		$html = "<span class='ui-widget-content'>".lang('text_method_1_parsing_arrays')."</span><br />";
		$html .= "<strong style='text-transform: Capitalize;'>".lang('term_country')."</strong>: " .$country['country_name'] . " (".$country['country_code'].")" . "<br />";
		$html .= "<strong>".lang('term_external_numbering_plan')."</strong>: " .$ext_route['name'] . " (".@$ext_route['dialling_code'].")" . "<br />";		
		$html .= "<strong>".lang('term_internal_numbering_plan')."</strong>: " .$int_route['name'] . " (".@$int_route['dialling_code'].")" . "<br />";
		$html .= "<strong>".lang('term_special_numbering_plan')."</strong>: " .$spe_route['name'] . " (".@$spe_route['dialling_code'].")" . "<br /><br />";		
		$html .= "<strong>".lang('term_processing_time')."</strong>: " . $time_difference  . '"';
		
		/* ***********************************************************************
		 * METHOD II
		 * --------
		 * GETTING ROUTES BY RUNNING MYSQL QUERIES WITH ORDERING ROUTES THAT BELONG
		 * TO THE DETECTED COUNTRY BY LENGTH OF `dialling_code` COLUMN AND LIMITING
		 * MYSQL RESPONSE TO 1 ENTRY (LIMIT 1)
		 * ***********************************************************************/
		
		// TIMING STARTS BEFORE FINDING THE ROUTES
		$start_time = microtime(TRUE);
			 
			 
		$routes_array = $this->PACKAGE->routing->numberingplan_routing->route($destination_number,'running_queries',$country['id']);			 
		
		$ext_route = $routes_array['ext_route'];
		$int_route = $routes_array['int_route'];
		$spe_route = $routes_array['spe_route'];
		
					
		// TIMING ENDS AFTER ROUTES ARE FOUND (FURTHER RESPONSE PROCESSING IS NOT INCLUDED IN TIME DIFFERENCE!!!)
		$end_time = microtime(TRUE);

		
		// CALCULATE TIME DIFFERENCE
		$time_difference = $end_time - $start_time;		
		$time_difference = round($time_difference,6);
		
		
		// PREPARE RESPONSE FOR SECOND METHOD
		$html .= "<br /><hr /><br />";
		$html .= "<span class='ui-widget-content'>".lang('text_method_2_running_query')."</span><br />";
		$html .= "<strong style='text-transform: Capitalize;'>".lang('term_country')."</strong>: " .$country['country_name'] . " (".$country['country_code'].")" . "<br />";
		$html .= "<strong>".lang('term_external_numbering_plan')."</strong>: " .$ext_route['name'] . " (".@$ext_route['dialling_code'].")" . "<br />";		
		$html .= "<strong>".lang('term_internal_numbering_plan')."</strong>: " .$int_route['name'] . " (".@$int_route['dialling_code'].")" . "<br />";
		$html .= "<strong>".lang('term_special_numbering_plan')."</strong>: " .$spe_route['name'] . " (".@$spe_route['dialling_code'].")" . "<br /><br />";		
		$html .= "<strong>".lang('term_processing_time')."</strong>: " . $time_difference  . '"';
		
		//$response = "false|".$country['country_name'].$start_time."-->".$end_time;
		$response = "true|".$html;
	}
	
	print $response;
}
}

?>
